<template>
  <div class="home-page">
    <!-- 首页内容 -->
    <div v-if="currentPage === 'home'" class="home-content">
    <!-- 标题栏 -->
    <div class="header-bar">
      <div class="back-button" @click="goToActivatedPage">
        <el-icon><ArrowLeft /></el-icon>
      </div>
      <div class="page-title">歌曲列表</div>
      <div class="placeholder"></div>
    </div>
    
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-input
        v-model="searchText"
        placeholder="搜索歌曲或歌手"
        prefix-icon="Search"
        class="search-input"
      />
    </div>

    <!-- 歌曲列表 -->
    <div class="song-list">
      <div
        v-for="(song, index) in songList"
        :key="song.id"
        class="song-item"
          @click="goToSongDetail(song)"
      >
        <div class="song-rank">{{ song.rank }}</div>
        <div class="song-info">
          <div class="song-title">{{ song.title }}</div>
          <div class="song-artist">{{ song.artist }}</div>
        </div>
        <div class="song-action">
          <el-icon 
            :class="['star-icon', { favorite: song.isFavorite }]"
              @click.stop="toggleFavorite(song)"
          >
              <StarFilled v-if="song.isFavorite" />
              <Star v-else />
          </el-icon>
          </div>
        </div>
      </div>

      <!-- VIP开通提示 -->
      <div v-if="showVIPBanner" class="vip-banner">
        <div class="vip-banner-left">
          <div class="crown-icon">
            <img src="../assets/crown.svg" alt="VIP" class="crown-img">
          </div>
          <span class="vip-text">开通VIP解锁全部功能</span>
        </div>
        <button class="vip-button" @click="handleVIPSubscription">立即开通</button>
        
        <!-- 关闭按钮 -->
        <div class="vip-close-btn" @click="closeVIPBanner">
          <span class="close-icon">×</span>
        </div>
      </div>
    </div>

    <!-- 歌曲详情页 -->
    <SongDetail 
      v-if="currentPage === 'detail' && !isFreeTrial" 
      :song-data="selectedSong"
      @back="goBack"
    />
    
    <!-- 未解锁歌曲详情页 -->
    <SongDetailLocked
      v-if="currentPage === 'detail' && isFreeTrial"
      :song-data="selectedSong"
      @back="goBack"
    />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Search, Star, StarFilled, ArrowLeft } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import SongDetail from './SongDetail.vue'
import SongDetailLocked from './SongDetailLocked.vue'
import { useRouter } from 'vue-router'

const searchText = ref('')
const currentPage = ref('home')
const isFreeTrial = ref(localStorage.getItem('isFreeTrial') === 'true')
const selectedSong = ref({})
const showVIPBanner = ref(true)
const router = useRouter()

onMounted(() => {
  // 检查是否从免费试用入口进入
  isFreeTrial.value = localStorage.getItem('isFreeTrial') === 'true'
  console.log('Is free trial:', isFreeTrial.value)
})

const songList = ref([
  { id: 1, rank: 50, title: '起风了', artist: '邓紫棋', isFavorite: false },
  { id: 2, rank: 49, title: '光年之外', artist: '邓紫棋', isFavorite: false },
  { id: 3, rank: 48, title: '句号', artist: '阿光', isFavorite: false },
  { id: 4, rank: 47, title: '这就是爱', artist: '张杰', isFavorite: false },
  { id: 5, rank: 46, title: '晴天', artist: '周杰伦', isFavorite: false },
  { id: 6, rank: 45, title: '最好的我们', artist: '王源', isFavorite: false },
  { id: 7, rank: 44, title: '生而为海', artist: '黄霄云', isFavorite: false },
  { id: 8, rank: 43, title: '无人之岛', artist: '任然', isFavorite: false },
  { id: 9, rank: 42, title: '我们的歌', artist: '王力宏', isFavorite: false }
])

const toggleFavorite = (song) => {
  song.isFavorite = !song.isFavorite
  
  if (song.isFavorite) {
    ElMessage({
      message: '收藏成功',
      type: 'success',
      duration: 2000,
      customClass: 'custom-message'
    })
  } else {
    ElMessage({
      message: '取消收藏',
      type: 'info',
      duration: 2000,
      customClass: 'custom-message'
    })
  }
}

const goToSongDetail = (song) => {
  selectedSong.value = song
  currentPage.value = 'detail'
}

const goBack = () => {
  currentPage.value = 'home'
}

// VIP订阅处理
const handleVIPSubscription = () => {
  router.push('/vip')
}

// 关闭VIP横幅
const closeVIPBanner = () => {
  showVIPBanner.value = false
}

// 返回到激活页面
const goToActivatedPage = () => {
  router.push('/activated')
}
</script>

<style scoped>
.home-page {
  min-height: 100vh;
  width: 100%;
  margin: 0 auto;
  background: linear-gradient(135deg, #3d3878 0%, #1a1732 100%);
  color: white;
  padding: 0;
  position: relative;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
}

/* 标题栏样式 */
.header-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
}

.back-button {
  font-size: 20px;
  cursor: pointer;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  color: rgba(255, 255, 255, 0.9);
}

.page-title {
  font-size: 18px;
  font-weight: 600;
  flex-grow: 1;
  text-align: center;
}

.placeholder {
  width: 40px;
}

/* 搜索栏样式 */
.search-bar {
  padding: 0 20px 16px;
  margin-top: -5px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.search-input {
  border-radius: 50px;
}

.search-input :deep(.el-input__wrapper) {
  background-color: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 50px;
  backdrop-filter: blur(15px);
  height: 44px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.search-input :deep(.el-input__inner) {
  color: white;
  font-size: 16px;
  height: 42px;
}

.search-input :deep(.el-input__inner::placeholder) {
  color: rgba(255, 255, 255, 0.7);
}

.search-input :deep(.el-input__prefix-inner) {
  color: rgba(255, 255, 255, 0.7);
}

/* 歌曲列表样式 */
.song-list {
  padding: 0 20px;
  margin-bottom: 80px;
}

.song-item {
  display: flex;
  align-items: center;
  padding: 16px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  transition: all 0.2s ease;
  cursor: pointer;
}

.song-item:hover {
  background-color: rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  margin: 0 -12px;
  padding: 16px 12px;
  transform: translateY(-1px);
}

.song-rank {
  width: 44px;
  text-align: center;
  font-size: 16px;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.9);
}

.song-info {
  flex: 1;
  margin-left: 16px;
  min-width: 0;
}

.song-title {
  font-size: 16px;
  font-weight: 600;
  color: white;
  margin-bottom: 4px;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.song-artist {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.7);
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.song-action {
  width: 44px;
  text-align: center;
}

.star-icon {
  font-size: 22px;
  color: rgba(255, 255, 255, 0.5);
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 50%;
  padding: 4px;
  min-width: 32px;
  min-height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.star-icon:hover {
  color: rgba(255, 255, 255, 0.8);
  background-color: rgba(255, 255, 255, 0.1);
  transform: scale(1.1);
}

.star-icon.favorite {
  color: #8B5CF6;
  background: rgba(139, 92, 246, 0.2);
  text-shadow: 0 0 8px rgba(139, 92, 246, 0.6);
  font-size: 24px;
  transform: scale(1.1);
  box-shadow: 0 4px 16px rgba(139, 92, 246, 0.3);
}

/* VIP横幅样式 */
.vip-banner {
  position: fixed;
  bottom: 20px;
  left: 20px;
  right: 20px;
  height: 68px;
  background: rgba(61, 56, 90, 0.95);
  border-radius: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 16px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(139, 92, 246, 0.3);
  z-index: 97;
}

/* 移动端安全区域适配 */
@supports (padding: max(0px)) {
  .vip-banner {
    bottom: max(20px, env(safe-area-inset-bottom) + 8px);
  }
}

.vip-banner-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.crown-icon {
  display: flex;
  align-items: center;
  justify-content: center;
}

.crown-img {
  width: 24px;
  height: 24px;
}

.vip-text {
  font-size: 16px;
  font-weight: 600;
  color: white;
}

.vip-button {
  background: linear-gradient(135deg, #8B5CF6 0%, #7C3AED 100%);
  border: none;
  border-radius: 24px;
  color: white;
  padding: 8px 20px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 4px 12px rgba(139, 92, 246, 0.3);
}

.vip-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(139, 92, 246, 0.4);
}

/* VIP横幅关闭按钮 */
.vip-close-btn {
  position: absolute;
  top: -10px;
  right: -10px;
  width: 22px;
  height: 22px;
  background: rgba(0, 0, 0, 0.6);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 98;
  transition: all 0.2s ease;
}

.vip-close-btn:hover {
  background: rgba(0, 0, 0, 0.8);
  transform: scale(1.1);
}

.close-icon {
  font-size: 16px;
  line-height: 16px;
  color: white;
  font-weight: 300;
  transform: translateY(-1px);
}

/* 小屏适配（≤375px） */
@media (max-width: 375px) {
  .home-page {
    max-width: 100%;
  }
  
  .search-bar {
    padding: 16px 16px 12px;
  }
  
  .song-list {
    padding: 0 16px;
  }
  
  .vip-banner {
    left: 16px;
    right: 16px;
    bottom: 16px;
  }
  
  .vip-text {
    font-size: 14px;
  }
  
  .vip-button {
    padding: 6px 16px;
    font-size: 12px;
  }
}

/* 超小屏（320px）适配 */
@media (max-width: 320px) {
  .search-bar {
    padding: 12px 12px 8px;
  }
  
  .song-list {
    padding: 0 12px;
  }
  
  .song-item {
    padding: 14px 0;
  }
  
  .song-rank {
    width: 40px;
    font-size: 15px;
  }
  
  .song-info {
    margin-left: 12px;
  }
  
  .song-title {
    font-size: 15px;
  }
  
  .song-artist {
    font-size: 13px;
  }
}

/* 自定义消息提示样式 */
:global(.custom-message) {
  background-color: rgba(45, 56, 120, 0.95) !important;
  border: 1px solid rgba(139, 92, 246, 0.3) !important;
  color: white !important;
  backdrop-filter: blur(10px) !important;
  border-radius: 12px !important;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4) !important;
  font-size: 16px !important;
  padding: 12px 20px !important;
  min-width: 120px !important;
  text-align: center !important;
  position: fixed !important;
  top: 50% !important;
  left: 50% !important;
  transform: translate(-50%, -50%) !important;
  z-index: 9999 !important;
}

:global(.custom-message .el-message__icon) {
  color: #8B5CF6 !important;
}
</style> 